﻿<template>
    <el-col :sm="24" :md="pzoption.mdwidth">
        <i class="iconfont icon-shezhi pull-right widgetset hidden-print" @click.stop="dialogInputVisible = true"></i>
        <i class="iconfont icon-shanchu pull-right widgetdel hidden-print" @click.stop="delWid(pzoption.wigdetcode)"></i>
        <el-form-item :label="pzoption.title" :prop="'wigetitems.' + index + '.value'" :rules="childpro.rules">
            <el-select placeholder="请选择学期" style="width:100%"  @change="selchange" v-model="pzoption.value" filterable>
                <el-option v-for="item in xqdata"
                           :key="item.xq"
                           :label="item.xq"
                           :value="item.xq">
                    <span style="float: left">{{ item.xq }}</span>
                    <span v-show="item.isnow=='Y'" style="float: right; color: #8492a6; font-size: 13px">当前 </span>
                </el-option>
            </el-select>
        </el-form-item>
        <el-dialog title="组件属性" :visible.sync="dialogInputVisible" width="40%">

            <el-form-item label="是否默认当前学期">
                <el-switch v-model="childpro.isnow" size="mini" style="width:100%">
                </el-switch>
            </el-form-item>
            <el-form-item label="下拉框更改时执行代码">
                <el-input v-model="childpro.jscode" :autosize="{ minRows: 6, maxRows: 9}" autocomplete="off" type="textarea"></el-input>
            </el-form-item>

        </el-dialog>
    </el-col>

</template>
<script>
    module.exports = {
        props: ['pzoption', 'index'],
        data: function () {
            return {
                dialogInputVisible: false,
                xqdata: [],
                childpro: {
                    name: "学期组件",
                    placeholder: "请选择",
                    isnow: true,
                    jscode: "",
                }
            }
        },
        methods: {
            delWid: function (wigdetcode) {
                // 子组件中触发父组件方法ee并传值cc12345
                this.$root.nowwidget = {};
                _.remove(this.$root.FormData.wigetitems, function (obj) {
                    return obj.wigdetcode == wigdetcode;
                });
            },
            selchange: function () {
                try {
                    var data = JSON.parse(JSON.stringify(this.pzoption));
                    let jscode = this.childpro.jscode;
                    let func = new Function('data', jscode);
                    func(data)
                } catch (e) {
                    app.$notify({
                        title: '成功',
                        message: '解析JS代码有误',
                        type: 'success'
                    });
                }

            },
            initwig: function () {
                var chi = this;
                //初始化数据
                chi.xqdata = JSON.parse(localStorage.getItem("xqdata"));
                //初始化数据

                //新增和设计页面处理默认值
                if (chi.$root.pagemodel == "0" || chi.$root.pagemodel == "1") {
                    if (chi.pzoption.childpro.isnow) {
                        chi.pzoption.value = _.find(chi.xqdata, function (obj) {
                            return obj.isnow == 'Y';
                        }).xq;
                    } else {
                        chi.pzoption.value = chi.xqdata[0].xq;
                    }
                }

            }
        },
        mounted: function () {
            var chi = this;
            chi.$nextTick(function () {
                //
                if (chi.$root.addchildwig) {
                    chi.$root.addchildwig();
                }
                if (chi.pzoption.childpro.name) {
                    //如果不是设计表单页面,检测新增属性
                    _.forIn(chi.childpro, function (value, key) {
                        if (!_.has(chi.pzoption.childpro, key)) {
                            chi.pzoption.childpro[key] = value;
                        }

                    });
                    chi.childpro = chi.pzoption.childpro;
                    //非第一次加载加载自身属性
                } else {
                    chi.pzoption.childpro = chi.childpro;
                    //第一次加载时把默认属性给组件
                }
                //初始化方法
                chi.initwig();
                //初始化方法
            })

        },
        watch: {

        }
    };
</script>